/**
 * 
 */
package com.risetek.tft.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.risetek.tft.entity.TDict;
import com.risetek.tft.service.DictService;
import com.zlinepay.framework.bean.TreeNode;
import com.zlinepay.framework.service.impl.EntityServiceImpl;

/**
 * @author  hetao
 *
 */
@Service
public class DictServiceImpl extends EntityServiceImpl<TDict> implements
		DictService {
	
	public List<TDict> listRoot(String type) {
		return find("from TDict a where a.TDict is null and a.type='"+type+"'");
	}
	
	private void d(Long [] s,Long f []){};
	public List<TreeNode> listTree(String type) {
		return createTree(find("from TDict a where a.TDict is null and a.type='"+type+"'"));
	}
	
	private List<TreeNode> createTree(List<TDict> l) {
		String hql = "from TDict a where a.TDict.id=?";
		List<TreeNode> tree = new ArrayList<TreeNode>();
		for (TDict a : l) {
			TreeNode t = new TreeNode();
			t.setId(a.getId().toString());
			t.setText(a.getMessage());
			List<TDict> list = find(hql, a.getId());
			if (!list.isEmpty()) {
				t.setChildren(createTree(list));
			}
			t.setState("open");//全打开
			tree.add(t);
		}
		return tree;
	}
}
